using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsDataBaice
{
    public class IconForm : Form
    {
        private Button[] iconButtons;
        private string[] moduleNames = {
            "挂号单设置", "门诊单设置", "住院单设置", "数据库备份",
            "科室管理", "权限管理", "员工添加", "数据恢复"
        };

        public IconForm()
        {
            InitializeComponent();
            SetupEventHandlers();
        }

        private void InitializeComponent()
        {
            this.Text = "系统功能模块";
            this.Size = new Size(900, 700);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = UIStyleManager.BackgroundColor;
            this.FormBorderStyle = FormBorderStyle.None;
            this.MaximizeBox = false;

            // 标题面板
            Panel titlePanel = new Panel
            {
                Dock = DockStyle.Top,
                Height = 80,
                BackColor = UIStyleManager.PrimaryColor
            };

            // 标题图标
            Label lblTitleIcon = new Label
            {
                Text = IconManager.Icons.Settings,
                Font = new Font("Segoe UI Emoji", 24),
                ForeColor = Color.White,
                Location = new Point(20, 25),
                AutoSize = true
            };

            // 标题
            Label lblTitle = new Label
            {
                Text = "系统功能模块",
                Font = UIStyleManager.TitleFont,
                ForeColor = Color.White,
                AutoSize = true,
                Location = new Point(60, 25)
            };

            titlePanel.Controls.Add(lblTitleIcon);
            titlePanel.Controls.Add(lblTitle);
            this.Controls.Add(titlePanel);

            // 创建图标按钮
            iconButtons = new Button[moduleNames.Length];
            int buttonWidth = 180;
            int buttonHeight = 140;
            int spacingX = 200;
            int spacingY = 160;

            // 计算总宽度和高度
            int totalWidth = 4 * buttonWidth + 3 * spacingX;
            int totalHeight = 2 * buttonHeight + spacingY;

            // 计算起始位置，使按钮组居中
            int startX = (this.ClientSize.Width - totalWidth) / 2;
            int startY = (this.ClientSize.Height - totalHeight) / 2 + 50; // 为标题留出空间

            for (int i = 0; i < moduleNames.Length; i++)
            {
                int row = i / 4;
                int col = i % 4;
                int x = startX + col * (buttonWidth + spacingX);
                int y = startY + row * (buttonHeight + spacingY);

                string icon = IconManager.GetModuleIcon(moduleNames[i]);
                iconButtons[i] = IconManager.CreateIconButton(moduleNames[i], icon, buttonWidth, buttonHeight);
                iconButtons[i].Location = new Point(x, y);
                iconButtons[i].Tag = i;

                this.Controls.Add(iconButtons[i]);
            }


        }

        private void SetupEventHandlers()
        {
            // 为每个图标按钮添加点击事件
            for (int i = 0; i < iconButtons.Length; i++)
            {
                int moduleIndex = i;
                iconButtons[i].Click += (s, e) => IconButton_Click(s, e, moduleIndex);
            }

            // 添加窗体大小改变事件，使按钮自适应居中
            this.Resize += IconForm_Resize;
        }

        private void IconForm_Resize(object sender, EventArgs e)
        {
            // 重新计算按钮位置
            RepositionButtons();
        }

        private void RepositionButtons()
        {
            if (iconButtons == null) return;

            int buttonWidth = 150;
            int buttonHeight = 120;
            int spacingX = 180;
            int spacingY = 150;

            // 计算总宽度和高度
            int totalWidth = 4 * buttonWidth + 3 * spacingX;
            int totalHeight = 2 * buttonHeight + spacingY;

            // 计算起始位置，使按钮组居中
            int startX = (this.ClientSize.Width - totalWidth) / 2;
            int startY = (this.ClientSize.Height - totalHeight) / 2;

            for (int i = 0; i < iconButtons.Length; i++)
            {
                int row = i / 4;
                int col = i % 4;
                int x = startX + col * (buttonWidth + spacingX);
                int y = startY + row * (buttonHeight + spacingY);

                iconButtons[i].Location = new Point(x, y);
            }
        }

        private void IconButton_Click(object sender, EventArgs e, int moduleIndex)
        {
            switch (moduleIndex)
            {
                case 0: // 挂号单设置
                    OpenFormInPanel(new RegisterTemplateForm());
                    break;
                case 1: // 门诊单设置
                    OpenFormInPanel(new OutpatientForm());
                    break;
                case 2: // 住院单设置
                    OpenFormInPanel(new InpatientForm());
                    break;
                case 3: // 数据库备份
                    MessageBox.Show("数据库备份功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case 4: // 科室管理
                    MessageBox.Show("科室管理功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case 5: // 权限管理
                    MessageBox.Show("权限管理功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case 6: // 员工添加
                    MessageBox.Show("员工添加功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case 7: // 数据恢复
                    MessageBox.Show("数据恢复功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }

        private void OpenFormInPanel(Form form)
        {
            // 隐藏当前窗体
            this.Hide();
            
            // 显示新窗体
            form.ShowDialog();
            
            // 关闭新窗体后重新显示当前窗体
            this.Show();
        }
    }
} 